package com.example.mobile2121606109ycg0611homeworka;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private ImageView ivMainBanner;
    private TextView tvMainContent;
    private String IMG="http://10.160.123.3:8080/A/img.jpg";//图片
    private String MSG="http://10.160.123.3:8080/A/msg.txt";//文本

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        new Thread(){
            @Override
            public void run() {
                super.run();
                try{
                    URL url=new URL(IMG);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    InputStream is = connection.getInputStream();
                    final Bitmap bitmap= BitmapFactory.decodeStream(is);
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            ivMainBanner.setImageBitmap(bitmap);
                        }
                    });
                    is.close();
                    connection.disconnect();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try{
                    URL url=new URL(MSG);
                    HttpURLConnection connection=(HttpURLConnection) url.openConnection();
                    InputStream is = connection.getInputStream();
                    InputStreamReader isr =new InputStreamReader(is);
                    BufferedReader br = new BufferedReader(isr);
                    String line=null;
                    String temp="";
                    while ((line=br.readLine())!=null){
                        temp+=line;
                        temp+="\n";
                    }
                    final String msg = temp;
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            tvMainContent.setText(msg);
                        }
                    });
                    is.close();
                    connection.disconnect();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void initView() {
        ivMainBanner = (ImageView) findViewById(R.id.ivMainBanner);
        tvMainContent = (TextView) findViewById(R.id.tvMainContent);
    }
}